import { DispImpl } from '@movinliao/h5-events'
import { yyapi, kRoots } from './api'
// import sha1 from 'sha1'
import { showMain, showLogin } from './utils'

const Page = DispImpl.extend(function () {
  // var self = this
  var _vue = null
  var _win = null
  var _auth = null
  var _im = null
  var _common = null

  var args = function () {
    var src = location.search || location.href || ''
    var str = src.slice(1)
    var num = str.indexOf('?')
    var ex = /([^&]+?)=([^#&]+)/g
    var result = {}

    str = str.substr(num + 1)
    while (ex.test(str)) {
      result[RegExp.$1] = RegExp.$2
    }

    return result || {}
  }

  // var routerCheck = function () {
  //   // self.openRouter(!videos.hasVideo() ? ROUTER.kEmpty : ROUTER.kMain)
  // }

  var onLoginSuccess = function (isReLogin) {
    console.info('=========================onLoginSuccess', isReLogin)
    showMain()
  }

  var onLoginError = function (msg, status, error) {
    console.info('=========================onLoginSuccess', msg, status, error)
    showLogin()
  }

  var onToBeKicked = function (code, reason) {
    console.info('=========================onLoginSuccess', code, reason)
    showLogin()
  }

  this.init = function () {
    yyapi.impl().init()
    _win = yyapi.impl().roots().get(kRoots.Win.Key)
    _auth = yyapi.impl().roots().get(kRoots.Auth.Key)
    _im = yyapi.impl().roots().get(kRoots.Im.Key)
    _common = yyapi.impl().roots().get(kRoots.Common.Key)
    _auth && _auth.onSignal('loginSuccess', onLoginSuccess)
    _auth && _auth.onSignal('loginError', onLoginError)
    _auth && _auth.onSignal('toBeKicked', onToBeKicked)
  }

  this.destroy = function () {
    yyapi.impl().destroy()
    _vue = null
  }

  this.start = function (vue) {
    yyapi.impl().start()
    _vue = vue
  }

  this.stop = function () {
    yyapi.impl().stop()
  }

  this.openRouter = function (path) {
    if (!_vue || (_vue.$router.path === path)) {
      return
    }
    var query = args()
    _vue && _vue.$router.push({ path, query })
  }

  this.vue = function () {
    return _vue
  }

  // kTopLeft = 0, kTopCenter = 1, kTopRight = 2,
  // kCenterLeft = 3, kCenter = 4, kCenterRight = 5,
  // kBottomLeft = 6, kBottomCenter = 7, kBottomRight = 8
  this.align = function (tag) {
    return _win && _win.excute('move', [tag])
  }

  this.move = function (x, y) {
    return _win && _win.excute('move', [x, y])
  }

  this.resize = function (w, h) {
    return _win && _win.excute('resize', [w, h])
  }

  this.setRect = function (x, y, w, h) {
    return _win && _win.excute('setRect', [x, y, w, h])
  }

  this.setMinSize = function (w, h) {
    return _win && _win.excute('setMinSize', [w, h])
  }

  this.maximize = function () {
    return _win && _win.excute('maximize', [])
  }

  this.minimize = function () {
    return _win && _win.excute('minimize', [])
  }

  this.restore = function () {
    return _win && _win.excute('restore', [])
  }

  this.close = function () {
    return _win && _win.excute('close', [])
  }

  this.show = function () {
    return _win && _win.excute('show', [])
  }

  this.hide = function () {
    return _win && _win.excute('hide', [])
  }

  this.setNCBorderWidth = function (w) {
    return _win && _win.excute('setNCBorderWidth', [w])
  }

  // [[x, y, w, h], [x, y, w, h]...]
  this.setCaptionArea = function (list) {
    return _win && _win.excute('setCaptionArea', [list])
  }

  this.addStyle = function (style) {
    return _win && _win.excute('addStyle', [style])
  }

  this.removeStyle = function (style) {
    return _win && _win.excute('removeStyle', [style])
  }

  this.setStyle = function (style) {
    return _win && _win.excute('setStyle', [style])
  }

  this.bringToFront = function () {
    return _win && _win.excute('bringToFront', [])
  }

  this.closeWindow = function () {
    try {
      self.stop()
      self.hide()
      self.destroy()
    } catch (error) {
    }
    window.close()
  }

  this.login = function (strAccount, password, imStatus, isRemeberPassword, isAutoLogin, isNewInput) {
    // password = sha1(password)
    // imStatus = imStatus || 0
    // isRemeberPassword = isRemeberPassword || false
    // isAutoLogin = isAutoLogin || false
    // isNewInput = isNewInput || false
    // _auth && _auth.excute('setAccountInfo', [strAccount, password, imStatus, isRemeberPassword, isAutoLogin, isNewInput])
    // _auth && _auth.excute('login', [])
    showMain()
  }

  this.logout = function () {
    _auth && _auth.excute('logout', [])
  }

  this.root = function (key) {
    return yyapi.impl().roots().get(key)
  }

  this.openWidow = function (url, width, height) {
    window.open(url, '_block', `width=${width},height=${height},resizable:1,titlebar:0,taskbaricon:1,defaultposition:4,backgroundcolor:3156776,forbidsystemclose:0,windowvisible:1,ncborderwidth:0`)
  }

  this.getMyUserInfo = function () {
    return _im && _im.excute('getMyUserInfo', [])
  }

  this.isLogined = function () {
    return _auth && _auth.excute('isLogined', [])
  }

  this.setConfig = function (key, value, uid) {
    return _common && _common.excute('setConfig', [key, value, uid])
  }

  this.getConfig = function (key, uid) {
    return _common && _common.excute('getConfig', [key, uid])
  }

  this.machineUuid = function () {
    return _common && _common.excute('machineUuid', [])
  }
})

export default Page
